package com.longrise.bjjt.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.longrise.bjjt.ui.AbstractSpinerAdapter.IOnItemSelectListener;
import com.longrise.bjjt_lib.R;

import java.util.List;

public class SpinerPopWindow extends PopupWindow implements OnItemClickListener {

    private Context mContext;
    private ListView mListView;
    private NormalSpinerAdapter mAdapter;
    private IOnItemSelectListener mItemSelectListener;
    private View mView;
    private boolean isWarpContent = false;

    private double multiple = 0; //固定倍数

    public void setMultiple(double multiple) {
        this.multiple = multiple;
    }

    public SpinerPopWindow(Context context, boolean isWarpContent) {
        super(context);
        mContext = context;
        this.isWarpContent = isWarpContent;
        init();
    }

    public void setItemListener(IOnItemSelectListener listener) {
        mItemSelectListener = listener;
    }

    @SuppressWarnings("deprecation")
    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, null);
        setContentView(view);
        setWidth(LayoutParams.WRAP_CONTENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.popwindow_anim_style);

        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);


        mListView = (ListView) view.findViewById(R.id.listview);
        if (!isWarpContent) {
            //设置listview的高度为屏幕的半个高度
            WindowManager window = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int height = window.getDefaultDisplay().getHeight();
            if (multiple == 0) {
                multiple = 0.5;
            }
            mListView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, (int) (height * multiple)));
        } else {
            //设置listview的高度为wrapcontent
            WindowManager window = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = window.getDefaultDisplay().getWidth();
            mListView.setLayoutParams(new LayoutParams((int) (width * 0.8), LayoutParams.WRAP_CONTENT));
        }
        mAdapter = new NormalSpinerAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

    }


    public void refreshData(List<String> list, int selIndex) {
        if (list != null && selIndex != -1) {
            mAdapter.refreshData(list, selIndex);
        }
    }

    public void setView(View view) {
        this.mView = view;
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
        dismiss();
        if (mItemSelectListener != null) {
            mItemSelectListener.onItemClick(pos, mView);
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {//解决在7.0上高度全屏时显示异常的问题
            int[] location = new int[2];
            anchor.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            showAtLocation(anchor, Gravity.NO_GRAVITY, x, y + anchor.getHeight());
            return;
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        backgroundAlpha(0.5f);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    //修改屏幕透明度
    public void backgroundAlpha(float bgAlpha) {
        Window window = ((Activity) mContext).getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        window.setAttributes(lp);
    }
}
